﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Diagnostics;

namespace PracticeAzure.Website.Shared
{
    public class PracticeAzureBaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // log the action request and parameters if in debug mode
            if (GlobalConfiguration.DebugMode)
            {
                var content = new StringBuilder();
                // append the controller name and action name
                content.AppendFormat("Controller Name: {0}; ", filterContext.ActionDescriptor.ControllerDescriptor.ControllerName)
                    .AppendFormat("Action Name: {0}; ", filterContext.ActionDescriptor.ActionName);
                // append the parameters' name and value
                filterContext.ActionParameters.Aggregate(content, (cont, param) =>
                    {
                        content.AppendFormat("Param {0} = {1}, ", param.Key, param.Value);
                        return content;
                    });
                // write log
                Trace.WriteLine(content.ToString(), "Debug");
            }
            // invoke base method
            base.OnActionExecuting(filterContext);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            // trace the exception
            Trace.WriteLine(filterContext.Exception.ToString(), "Error");
            base.OnException(filterContext);
        }
    }
}
